//
// Created by 24087 on 2025/5/11.
//

#include "circular_queue.h"

const uint8_t MAX_SIZE = 8;

void QueueInit(Queue *queue)
{
	queue->data = malloc(sizeof(QE_DataType) * MAX_SIZE);
	queue->head = 0;
	queue->tail = 0;
	queue->size = 0;
	queue->capacity = MAX_SIZE;
}

void QEDestroy(Queue *queue)
{
	if (queue->data)
	{
		free(queue->data);
		queue->data = NULL;
	}
	queue->head = 0;
	queue->tail = 0;
	queue->size = 0;
	queue->capacity = 0;
}

void QEPush(Queue *queue, const QE_DataType data)
{
	if (QueueFull(queue))
	{
		printf("Queue is full\n");
		return;
	}

	queue->data[queue->tail] = data;
	queue->tail = (queue->tail + 1) % queue->capacity;
	queue->size++;
	printf("push done with %d\n", data);
}

bool QueueFull(const Queue *queue)
{
	if (queue->size == queue->capacity)
	{
		return true;
	}
	return false;
}

QE_DataType QEPop(Queue *queue)
{
	if (QueueEmpty(queue))
	{
		printf("Queue is empty\n");
		return NAN;
	}

	const QE_DataType temp = queue->data[queue->head];
	queue->head = (queue->head + 1) % queue->capacity;
	queue->size--;
	printf("pop done with %d\n", temp);
	return temp;
}

bool QueueEmpty(const Queue *queue)
{
	return queue->size == 0;
}
